<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="原密码" prop="spassword">
        <el-input type="password" v-model="form.spassword"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="npassword">
        <el-input type="password" v-model="form.npassword"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="dpassword">
        <el-input type="password" v-model="form.dpassword"></el-input>
      </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="onSubmit">提交修改</el-button>
    </el-form-item>
    </el-form>
  </div>
</template>

<script>
    import {password as userPwd} from "@/api/user";
    import {password as studentPwd} from "@/api/student";
    import {password as companyPwd} from "@/api/company";
    import local from "@/store/local";
    import {message} from "@/utils/message";

    export default {
        name: "Password",
        props:{
            param:{
                type:Object,
                default:()=>{}
            }
        },
        data(){
          let validatePwd = (rule,value,callback)=>{
            if (value === '') {
              callback(new Error('请再次输入密码'));
            } else if (value !== this.form.npassword) {
              callback(new Error('两次输入密码不一致!'));
            } else {
              callback();
            }
          }
            return{
                form:{
                    id:'',
                    spassword:'',
                    npassword:'',
                    dpassword:'',
                },
                rules:{
                  spassword:[{required:true,message:'必填项不能为空'}],
                  npassword:[{required:true,message:'必填项不能为空'}],
                  dpassword:[{required:true,message:'必填项不能为空'},{required:true,validator:validatePwd}],
                }
            }
        },
        methods:{
          onSubmit(){
            this.$refs['form'].validate(valid=>{
              if(valid){
                this.form.id = local.getUserInfo().id;
                //管理员
                if(local.getUserInfo().type == 0){
                  userPwd(this.form).then(res=>{
                    message.success(res.msg);
                  }).catch(error=>{})
                }
                //企业
                if(local.getUserInfo().type == 1){
                  companyPwd(this.form).then(res=>{
                    message.success(res.msg);
                  }).catch(error=>{})
                }
                //学生
                if(local.getUserInfo().type == 2){
                  studentPwd(this.form).then(res=>{
                    message.success(res.msg);
                  }).catch(error=>{})
                }
              }
            })
          }
        }
    }
</script>

<style scoped>

</style>